import axios from 'axios';
import { Toast } from 'antd-mobile';

let opt={}
console.log(process.env.NODE_ENV)
if(process.env.NODE_ENV == 'development'){
  opt={
    baseURL: 'https://cnodejs.org/api/v1',
    timeout: 3000,
  }
}else if(process.env.NODE_ENV == 'production'){
  opt={
    baseURL: 'https://baidu.com',
    timeout: 3000,
  }
}

let Axios = axios.create(opt);

// 添加请求拦截器
Axios.interceptors.request.use(function (config) {
  console.log('请求拦截');
  const userInfo=localStorage.getItem('userInfo');
  const token=userInfo&&JSON.parse(userInfo).token;
  config.headers['Authorization'] = token;
  return config;
}, function (error) {
  return Promise.reject(error);
});

// 添加响应拦截器
Axios.interceptors.response.use(function (response) {
  console.log(response);
  if (response.status == 200) {
    return response.data;
  }
}, function (error) {
  console.log(error)
  Toast.show({
    icon: 'fail',
    content: error.response.data.error_msg,
  })
  return Promise.reject(error);
});

export default Axios;
